import http from '../utils/http'

// 用户环境管理
export const createUserEnvironment = (userId, params) =>
	http.post(`/api/users/${encodeURIComponent(userId)}/environment`, null, { params })

export const createSelectiveUserEnvironment = (userId, params) =>
	http.post(`/api/users/${encodeURIComponent(userId)}/environment/selective`, null, { params })

export const startUserEnvironment = (userId) =>
	http.post(`/api/users/${encodeURIComponent(userId)}/environment/start`)

export const stopUserEnvironment = (userId) =>
	http.post(`/api/users/${encodeURIComponent(userId)}/environment/stop`)

export const deleteUserEnvironment = (userId, operatorRole) =>
	http.delete(`/api/users/${encodeURIComponent(userId)}/environment`, { params: { operatorRole } })

export const getUserEnvironment = (userId) =>
	http.get(`/api/users/${encodeURIComponent(userId)}/environment`)

export const getUserAccessInfo = (userId) =>
	http.get(`/api/users/${encodeURIComponent(userId)}/environment/access`)

export const isUserEnvironmentExists = (userId) =>
	http.get(`/api/users/${encodeURIComponent(userId)}/environment/exists`)

// 共享 IDE 环境
export const createSharedIdeEnvironment = (body) =>
	http.post('/api/users/shared/environment/ide', body)

export const deleteSharedIdeEnvironment = (groupId, operatorRole) =>
	http.delete('/api/users/shared/environment/ide', { params: { groupId, operatorRole } })

export const generateSharedIdeNginx = (params) =>
	http.get('/api/users/shared/environment/ide/nginx', { params })

// 资源管理
export const getUserResourceUsage = (userId) =>
	http.get(`/api/users/${encodeURIComponent(userId)}/resources`)

export const setUserResourceLimits = (userId, resourceLimits) =>
	http.put(`/api/users/${encodeURIComponent(userId)}/resources/limits`, resourceLimits)

export default {
	createUserEnvironment,
	createSelectiveUserEnvironment,
	startUserEnvironment,
	stopUserEnvironment,
	deleteUserEnvironment,
	getUserEnvironment,
	getUserAccessInfo,
	isUserEnvironmentExists,
	createSharedIdeEnvironment,
	deleteSharedIdeEnvironment,
	generateSharedIdeNginx,
	getUserResourceUsage,
	setUserResourceLimits,
}

